<?php
	if (!session_start()) session_start();
	if (!isset($_SESSION['kiemtradangnhapthinguyet']) || $_SESSION['kiemtradangnhapthinguyet']!=1) {
		header('location: ../../../index.php');
		die();
	}
	$table = "user";
	$id = $_GET['id'];
	if (isset($_POST["id"]) && $_POST["id"]!="")
		$id = $_POST['id'];
	$record = $class_user->get_by_thutu($id);
	
	$check_isset = array(
		'id',
		'old_password',
		'new_password',
		'hoten',
		'diachi',
		'dienthoai',
		'email'
	);
	$check_empty = array(
		'id',
		'old_password',
		'new_password'
	);
	if ($db->check_isset($check_isset)) {
		$username = $record["username"];
		$password = $record["password"];
		$thutu = $record["thutu"];
		$ngay = $record["ngay"];
		$idgroup = $record["idgroup"];
		$anhien = $record["anhien"];
		
		if ($db->check_empty($check_empty)) {
			list($md5pass, $saltpass) = explode (":", $password);
      		if ((md5($_POST["old_password"] . $saltpass)) == $md5pass) {
				for ($i=0; $i<=32; $i++) {
					$d=rand(1,30)%2;
					$salt .= $d ? chr(rand(65,90)) : chr(rand(48,57));
				}
				$hashed = md5($_POST["new_password"] . $salt);
				$encrypted = $hashed . ':' . $salt;
				
				$record = new user;
				$record->dt_set_id($id);
				$record->dt_set_username($username);
				$record->dt_set_password($encrypted);
				$record->dt_set_hoten($_POST["hoten"]);
				$record->dt_set_diachi($_POST["diachi"]);
				$record->dt_set_dienthoai($_POST["dienthoai"]);
				$record->dt_set_email($_POST["email"]);
				$record->dt_set_idgroup($idgroup);
				$record->dt_set_thutu($thutu);
				$record->dt_set_ngay($ngay);
				$record->dt_set_anhien($anhien);
				
				$record->update();
				?>
					<script language="javascript">alert("Sửa dữ liệu thành công!");</script>
				<?php
				location('index.php?p='.$table.'_quanly');
      		}else {
      			?>
					<script language="javascript">alert("Password is not correct!");</script>
				<?php
      		}
		}else {
			$record = new user;
			$record->dt_set_id($id);
			$record->dt_set_username($username);
			$record->dt_set_password($password);
			$record->dt_set_hoten($_POST["hoten"]);
			$record->dt_set_diachi($_POST["diachi"]);
			$record->dt_set_dienthoai($_POST["dienthoai"]);
			$record->dt_set_email($_POST["email"]);
			$record->dt_set_idgroup($idgroup);
			$record->dt_set_thutu($thutu);
			$record->dt_set_ngay($ngay);
			$record->dt_set_anhien($anhien);
				
			$record->update();
			?>
				<script language="javascript">alert("Sửa dữ liệu thành công!");</script>
			<?php
			location('index.php?p='.$table.'_quanly');
		}
		
	}

?>				
            	<div class="dangnhap_title">
                <a href="index.php" style="color:#F8991D;" title="Trang chủ">Trang chủ</a>
                <span style="font-size:9px;">>></span> 
                Hệ thống
            	</div>
            	<form method="post" action="index.php?p=<?php echo $table; ?>_manager_sua">
            	<input type="hidden" name="id" value="<?php echo $id; ?>" />
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        Vietnamese's Information
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Username:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="username" style="width:520px; padding:0px;" value="<?php echo $record['username']; ?>" disabled="disabled" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Old Password:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="password" name="old_password" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- New Password:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="password" name="new_password" style="width:520px; padding:0px;" />
                    </div>
                </div>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        More Information
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Full name:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="hoten" style="width:520px; padding:0px;" value="<?php echo $record['hoten']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Phone:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="dienthoai" style="width:520px; padding:0px;" value="<?php echo $record['dienthoai']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Adress:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="diachi" style="width:520px; padding:0px;" value="<?php echo $record['diachi']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Email:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="email" style="width:520px; padding:0px;" value="<?php echo $record['email']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_input">
                        <input type="image" src="imgs/save_button.png" border="0" />
                        <a href="javascript:history.go(-1);"><img src="imgs/exit_button.png" border="0" /></a>
                    </div>
                </div>
               	</form>